<?php
$openid = $_POST["openid"];
function connect_database($database="user_database"){
  $conn = new mysqli("localhost", "root", "1314", $database);
  if ($conn->connect_error) {// 检测连接
      die("内部错误： " . $conn->connect_error);
  }
  return $conn;
}

function find_data($openid){
  $conn = connect_database();
  $sql = "SELECT userlibrary FROM openidlibrary WHERE openid_wx='$openid'"; //SELECT=列  FPOM=表 WHERE=条件
  $result = mysqli_query($conn,$sql);  //查找数组
  $result = mysqli_fetch_assoc($result); //获取数组
  if(isset($result))
  {
      //$dax = implode('',$result);//数组转字符
      return implode('',$result);
  }else {
  return "vacuous";
  }
  $conn->close();
}
function find_pages($userlibrary){
  $conn = connect_database();
  $sql = "SELECT applianceId FROM $userlibrary"; //SELECT=列  FPOM=表 WHERE=条件
  $result =  $conn->query($sql);  //查找数组
    if($result->num_rows > 0){
        while($row = mysqli_fetch_assoc($result)) {
            $row = implode('',$row);//数组转字符
            $pages[] = array(
              'applianceId'=>$row,
              'image_1'=>find_e('image_1',$userlibrary,$row),
              'image_2'=>find_e('image_2',$userlibrary,$row),
              'lamp'=> find_e('lamp',$userlibrary,$row),
              'name'=>find_e('groupName	',$userlibrary,$row) . find_e('device_name',$userlibrary,$row),
            );
            //读取的单条字符串转为数组
            //$dis[] = json_decode($row["discoveredAppliances"],true);
        }
    }else{
      $pages[] = array(
        'applianceId'=> 0,
        'image_1'=> '../../png/fan_shut.png',
        'lamp'=> 'add_',
        'name'=>'添加' 
      );
    }
    return $pages;
    $conn->close();
}
function find_e($column,$userlibrary,$applianceId){
  $conn = connect_database();
  $sql = "SELECT $column FROM $userlibrary WHERE applianceId='$applianceId'"; //SELECT=列  FPOM=表 WHERE=条件
  $result = mysqli_query($conn,$sql);  //查找数组
  $result = mysqli_fetch_assoc($result); //获取数组
  if(isset($result))
  {
      //$dax = implode('',$result);//数组转字符
      return implode('',$result);
  }else {
  return "vacuous";
  }
  $conn->close();
}
echo json_encode(find_pages(find_data($openid)));
?>